const app = getApp()
/* 背景音乐 */
const xBackMusic = wx.getBackgroundAudioManager();
Page({
    data: {
        audio: {},
        play: true, // true : 播放 ｜ false : 暂停
        star: '00:00',
        end: '00:00',
        treatment_item_id:'',
        day:''
    },
    onShareAppMessage: function () {
      let that = this
      return {
        title: that.data.audio.name,
        url: '/subtool/pages/rudio/detail/index?audio_id='+that.data.audio._id,
        imageUrl:'https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/shareSleep.png'
      };
    },
    // onShareTimeline:function(){
    //   let that = this
    //   return {
    //     title: that.data.audio.name,
    //     url: '/subtool/pages/rudio/detail/index?audio_id='+that.data.audio._id,
    //     imageUrl:'https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/shareSleep.png'
    //   };
    // },
    xChildReload: function () {
      var pages = getCurrentPages();
      if (pages.length > 1) {
        var beforePage = pages[pages.length - 2];
        if(beforePage.data.xProductnew){
          beforePage.xInfo();
        }else{
          var beforePage = pages[pages.length - 3];
          beforePage.xInfo();
        }
      }
    },
    onUnload:function(){
      wx.getBackgroundAudioManager().stop()
    },
    onReady:function(){
        // xBackMusic.src='1'
    },
    onLoad: function (options) {
        let that = this
        that.setData({
          week: options.week?Number(options.week):'',
          treatment_item_id:options.treatment_item_id?options.treatment_item_id:''
        })
        that.xInit(options.audio_id)
    },
    xInit:function(id){
      let that = this
      wx.cloud.callFunction({
        name: 'digital_therapy_extend',
        data: {
          action: "get_muse_info",
          _id:id
        }
      }).then(res => {
          if(res.result.length>0){
            let data = res.result[0]
            xBackMusic.src = data.audio
            xBackMusic.title = data.name
            that.xAudioPlay()
            that.setData({
              audio:data
            })
          }
      })
    },
    xAudioPlay: function () {
        let that = this
        let data = that.data.audio
        // 设置非自动播放
        // xBackMusic.src =data.file
        // xBackMusic.title = data.name
        // xBackMusic.singer = data.ator
        // xBackMusic.coverImgUrl = data.cover
        
        xBackMusic.play()
        that.setData({
            play: false
        })
        setTimeout(() => {
            xBackMusic.onTimeUpdate(() => {
                let xNowTime = xBackMusic.duration - xBackMusic.currentTime
                let time = that.formatSeconds(xNowTime)
                let alltime = that.formatSeconds(xBackMusic.duration)
                that.setData({
                    end: time,
                    star:alltime
                })
            })
        }, 1000)
        xBackMusic.onPause(()=>{
          this.setData({
            play: true
          })
        })
         xBackMusic.onEnded(() => {
            let _dataObj = {
                week: that.data.week,
                job_type: 3,
                treatment_item_id:that.data.treatment_item_id,
                day:that.data.day,
                meditation_exercises_return: {
                    meditation_id: that.data.audio._id,
                    meditation_name: that.data.audio.name,
                    meditation_duration: that.data.end,
                    is_complete: true
                }
            }
             wx.cloud.callFunction({
                name: 'digital_therapy',
                data: {
                    action: "add_job_record",
                    dataObj: _dataObj
                },
                success: () => {
                    that.xChildReload()
                    wx.navigateBack({
                        delta: 1,
                    })
                }
            })
        })
    },
    formatSeconds(value) {
        var theTime = parseInt(value);// 秒
        var middle= 0;// 分
        var hour= 0;// 小时
        if(theTime > 60) {undefined
            middle= parseInt(theTime/60);
            theTime = parseInt(theTime%60);
            if(middle> 60) {undefined
                hour= parseInt(middle/60);
                middle= parseInt(middle%60);
            }
        }
        let h = parseInt(hour) > 0 ? parseInt(hour) + ":"  : ''
        let m = parseInt(middle) > 9 ? parseInt(middle) : '0' + parseInt(middle)
        let s = parseInt(theTime) > 9 ? parseInt(theTime) : '0' + parseInt(theTime);
        let result = h + m + ':' + s
        return result;
    },
    xAudioStop: function () {
        let that = this
        xBackMusic.pause()
        that.setData({
            play: true
        })
    },
    xEnded: function () {
        let that = this
        xBackMusic.onEnded(() => {
            let _dataObj = {
                week: that.data.week,
                job_type: 3,
                treatment_item_id:that.data.treatment_item_id,
                day:that.data.day,
                meditation_exercises_return: {
                    meditation_id: that.data.audio._id,
                    meditation_name: that.data.audio.name,
                    meditation_duration: that.data.end,
                    is_complete: true
                }
            }
             wx.cloud.callFunction({
                name: 'digital_therapy',
                data: {
                    action: "add_job_record",
                    dataObj: _dataObj
                },
                success: () => {
                    that.xChildReload()
                    wx.navigateBack({
                        delta: 1,
                    })
                }
            })
        })
    }
})